import { workDict } from '../../../db/models/workDict';  // 假设你有一个用户模型
import {sendError, sendSuccess} from "../../utils/responseHandler";

export default async function handler(req, res) {
    if (req.method === 'GET') {
        try {
            const { username, password } = req.body;

            // 在数据库中查找用户
            const dictData = await workDict.findAll();
            const typeLabels = {
                1: '全栈',
                2: '前端',
                3: '后端'
            };
            // 将数据按 type 分组
            const options = dictData.reduce((acc, item) => {
                const typeGroup = acc.find(group => group.type === item.type);
                if (typeGroup) {
                    typeGroup.options.push({ label: item.label, value: item.value });
                } else {
                    acc.push({
                        type: item.type,
                        label: typeLabels[item.type],
                        options: [{ label: item.label, value: item.value }]
                    });
                }
                return acc;
            }, []);

            sendSuccess(res, { options });

            sendSuccess(res, dictData);
        } catch (error) {
            sendError(res, '服务器错误', 500);
        }
    } else {
        res.setHeader('Allow', ['GET']);
        res.status(405).end('Method Not Allowed');
    }
}
